package com.dong.untils;
/*
图片处理
 */

import com.dong.pojo.Iidar;
import com.dong.pojo.Image;
import java.io.*;

/*
数据库图片操作
 */
public class ProcessImage {

    //数据库插入图片
    public void insertImage(String path){
        try{
            File file = new File(path);
            long length = file.length();//获取图片的大小
            FileInputStream fis = new FileInputStream(path);//图片的输入流
            Image image = new Image();
            image.setName("第8张图片");
            byte[] photo = new byte[(int) length];
            fis.read(photo);
            image.setPhoto(photo);
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //取出数据库的图片到指定文件夹
    public void getImage(String path,Image image){
        try{
            byte[] photo = image.getPhoto();
            InputStream in = new ByteArrayInputStream(photo);
            FileOutputStream fos = new FileOutputStream(path);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len=in.read(buf))!= -1) {
                fos.write(buf,0,len);
            }
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}